<?php



/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Gmap
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
 
 
 
class Gmap_Form_Search extends Fields_Form_Search
{
  protected $_fieldType = 'gmap';
  
  
  
  public function init()
  {
    parent::init();

    $this->loadDefaultDecorators();

    $this
      ->setAttribs(array(
        'id' => 'filter_form',
        'class' => 'gmaps_browse_filters field_search_criteria',
      ))
      ->getDecorator('HtmlTag')
        ->setOption('class', 'browsegmaps_criteria gmaps_browse_filters');
    
    $this->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array(),'gmap_browse',true));
    
    // Add custom elements
    $this->getAdditionalOptionsElement();
  }

  public function getAdditionalOptionsElement()
  {
    $i = -5000;
    
    $this->addElement('Hidden', 'page', array(
      'order' => $i++,
    ));

    $this->addElement('Hidden', 'tag', array(
      'order' => $i++,
    ));
    
    
    $this->addElement('Text', 'search', array(
      'label' => 'Keyword',
      'order' => $i++,
      'decorators' => array(
        'ViewHelper',
        array('Label', array('tag' => 'span')),
        array('HtmlTag', array('tag' => 'li'))
      ),
    ));  
    
    $this->addElement('Text', 'location', array(
      'label' => 'Location',
      'order' => $i++,
      'validators' => array(
        new Radcodes_Lib_Validate_Location_Address(),
      ),
      'decorators' => array(
        'ViewHelper',
        array('Label', array('tag' => 'span')),
        array('HtmlTag', array('tag' => 'li'))
      ),
    ));    
    
    $unit = Engine_Api::_()->getApi('settings', 'core')->getSetting('gmap.distanceunit', Radcodes_Lib_Helper_Unit::UNIT_MILE);
    
    $distances = array('0'=>"");
    $distance_ranges = array(1,5,10,25,50,100,250,500,1000);
    foreach ($distance_ranges as $distance) {
      $distances[$distance] = "$distance $unit";
    }
    
    $this->addElement('Select', 'distance', array(
      'label' => 'Within Distance',
      'order' => $i++,
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ),    
      'multiOptions' => $distances,
      'decorators' => array(
        'ViewHelper',
        array('Label', array('tag' => 'span')),
        array('HtmlTag', array('tag' => 'li'))
      ),
    ));
    
    $categories = Engine_Api::_()->gmap()->getCategories();
    $categories_prepared = Engine_Api::_()->gmap()->convertCategoriesToArray($categories);
    $categories_prepared = array(""=>"All Categories") + $categories_prepared;
    
    $this->addElement('Select', 'category', array(
      'label' => 'Category',
      'order' => $i++,
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ),
      'multiOptions' => $categories_prepared,
      //'onchange' => 'this.form.submit();',
      'decorators' => array(
        'ViewHelper',
        array('Label', array('tag' => 'span')),
        array('HtmlTag', array('tag' => 'li'))
      ),
    ));    
    
    $this->addElement('Select', 'show', array(
      'label' => 'Show',
      'order' => $i++,
      'multiOptions' => array(
        '' => 'Everyone\'s Posts',
        '1' => 'Only My Friends\' Posts',
      ),
      //'onchange' => 'this.form.submit();',
      'decorators' => array(
        'ViewHelper',
        array('Label', array('tag' => 'span')),
        array('HtmlTag', array('tag' => 'li'))
      ),
    ));    
    
    $this->addElement('Select', 'order', array(
      'label' => 'Browse By',
      'order' => $i++,
      'multiOptions' => array(
        'creation_date' => 'Most Recent',
        'view_count' => 'Most Viewed',
        'comment_count' => 'Most Commented',
        'modified_date' => 'Last Updated'
      ),
      //'onchange' => 'this.form.submit();',
      'decorators' => array(
        'ViewHelper',
        array('Label', array('tag' => 'span')),
        array('HtmlTag', array('tag' => 'li'))
      ),
    ));


   
    
  
    
    if (count($this->_fieldElements)) {
      $this->_order['separator1'] = $i++;
    }
    else {
      $this->removeElement('separator1');
    }    
    
    $j = 10000000;  
    
    
    $this->addElement('Checkbox', 'featured', array(
      'label' => 'Only Featured Gmaps',
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ), 
      'decorators' => array(
        'ViewHelper',
        array('Label', array('placement' => 'APPEND', 'tag' => 'label')),
        array('HtmlTag', array('tag' => 'li'))
      ),
      'order' => $j++,
    ));

    $this->addElement('Checkbox', 'sponsored', array(
      'label' => 'Only Sponsored Gmaps',
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ), 
      'decorators' => array(
        'ViewHelper',
        array('Label', array('placement' => 'APPEND', 'tag' => 'label')),
        array('HtmlTag', array('tag' => 'li'))
      ),
      'order' => $j++,
    ));
    
    $this->addElement('Checkbox', 'home', array(
      'label' => 'Only User Homes',
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ), 
      'decorators' => array(
        'ViewHelper',
        array('Label', array('placement' => 'APPEND', 'tag' => 'label')),
        array('HtmlTag', array('tag' => 'li'))
      ),
      'order' => $j++,
    ));     
    
    $this->addElement('Button', 'done', array(
      'label' => 'Search',
      'type' => 'submit',
      'ignore' => true,
      'order' => $j++,
      'decorators' => array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'li'))
      ),
    ));
    
  }
  
  /*
  public function init()
  {
    //parent::init();

    $this->loadDefaultDecorators();


    $this->getDecorator('HtmlTag')->setOption('class', 'browsegmaps_criterian gmaps_browse_filters')->setOption('id', 'filter_form');
    
    $this
      ->setAttribs(array(
        'id' => 'filter_form',
        'class' => 'global_form_box gmaps_browse_filters',
      ))
      ->setMethod('get')
      ->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array(),'gmap_browse',true))
      ;
    
    // Generate
    $this->generate();
      
    // Add custom elements
    $this->getAdditionalOptionsElement();      
      


    foreach( $this->getFieldElements() as $fel ) {
      if( $fel instanceof Zend_Form_Element ) {
      	
        $fel->clearDecorators();
        $fel->addDecorator('ViewHelper');
        Engine_Form::addDefaultDecorators($fel);
        
      } else if( $fel instanceof Zend_Form_SubForm ) {
      	$fel->clearDecorators();
        $fel->setDescription('<label>' . $fel->getDescription() . '</label>');
        $fel->addDecorator('FormElements')
            ->addDecorator('HtmlTag', array('tag' => 'div', 'id'  => $fel->getName() . '-element', 'class' => 'form-element'))
            ->addDecorator('Description', array('tag' => 'div', 'class' => 'form-label', 'placement' => 'PREPEND', 'escape' => false))
            ->addDecorator('HtmlTag2', array('tag' => 'div', 'id'  => $fel->getName() . '-wrapper', 'class' => 'form-wrapper browse-range-wrapper'));
        
      }
    }
    

  }
  
  public function getAdditionalOptionsElement()
  {
    $i = -100;
    
    $this->addElement('Text', 'search', array(
      'label' => 'Keyword',
      'order' => $i++,
    ));  
    
    $this->addElement('Text', 'location', array(
      'label' => 'Location',
      'order' => $i++,
      'validators' => array(
        new Radcodes_Lib_Validate_Location_Address(),
      ),
    ));    
    
    $unit = Engine_Api::_()->getApi('settings', 'core')->getSetting('gmap.distanceunit', Radcodes_Lib_Helper_Unit::UNIT_MILE);
    
    $distances = array('0'=>"");
    $distance_ranges = array(1,5,10,25,50,100,250,500,1000);
    foreach ($distance_ranges as $distance) {
      $distances[$distance] = "$distance $unit";
    }
    
    $this->addElement('Select', 'distance', array(
      'label' => 'Within Distance',
      'order' => $i++,
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ),    
      'multiOptions' => $distances,
    ));
    
    $categories = Engine_Api::_()->gmap()->getCategories();
    $categories_prepared = Engine_Api::_()->gmap()->convertCategoriesToArray($categories);
    $categories_prepared = array(""=>"All Categories") + $categories_prepared;
    
    $this->addElement('Select', 'category', array(
      'label' => 'Category',
      'order' => $i++,
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ),
      'multiOptions' => $categories_prepared,
      'onchange' => 'this.form.submit();',
      
    ));    
    
    $this->addElement('Select', 'show', array(
      'label' => 'Show',
      'order' => $i++,
      'multiOptions' => array(
        '' => 'Everyone\'s Posts',
        '1' => 'Only My Friends\' Posts',
      ),
      'onchange' => 'this.form.submit();',
    ));    
    
    $this->addElement('Select', 'order', array(
      'label' => 'Browse By',
      'order' => $i++,
      'multiOptions' => array(
        'creation_date' => 'Most Recent',
        'view_count' => 'Most Viewed',
        'comment_count' => 'Most Commented',
        'modified_date' => 'Last Updated'
      ),
      'onchange' => 'this.form.submit();',
    ));


    $this->addElement('Checkbox', 'featured', array(
      'label' => 'Only Featured Gmaps',
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ), 
      'decorators' => array(
        'ViewHelper',
        array('Label', array('placement' => 'APPEND', 'tag' => 'label')),
        array('HtmlTag', array('tag' => 'div', 'id'  => 'featured-element', 'class'=>'form-wrapper'))
      ),
      'order' => 200
    ));

    $this->addElement('Checkbox', 'sponsored', array(
      'label' => 'Only Sponsored Gmaps',
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ), 
      'decorators' => array(
        'ViewHelper',
        array('Label', array('placement' => 'APPEND', 'tag' => 'label')),
        array('HtmlTag', array('tag' => 'div', 'id'  => 'sponsored-element', 'class'=>'form-wrapper'))
      ),
      'order' => 201
    ));
    
    $this->addElement('Checkbox', 'home', array(
      'label' => 'Only User Homes',
      'filters' => array(
        new Radcodes_Lib_Filter_Null()
      ), 
      'decorators' => array(
        'ViewHelper',
        array('Label', array('placement' => 'APPEND', 'tag' => 'label')),
        array('HtmlTag', array('tag' => 'div', 'id'  => 'home-element', 'class'=>'form-wrapper'))
      ),
      'order' => 202
    ));    
    
    $this->addElement('Hidden', 'page', array(
      'order' => 500
    ));

    $this->addElement('Hidden', 'tag', array(
      'order' => 501
    ));
        
    
    $this->addElement('Button', 'done', array(
      'label' => 'Search',
      'onclick' => 'this.form.submit();',
      'order' => 1000
    ));
  }
  */
}